package com.jy.adscm.hcPlan.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.hcPlan.bean.HcPlanMakePartMateriel;
import com.jy.adscm.hcPlan.bean.Hc_plan_materiel_other_config;
import com.jy.adscm.hcPlan.bean.Hc_plan_materiel_other_config_example;
import com.jy.adscm.hcPlan.mapper.mysql.HcPlanMakePartMaterielMapper;
import com.jy.adscm.hcPlan.mapper.mysql.Hc_plan_materiel_other_configMapper;
import com.jy.adscm.hcPlan.mapper.mysql.Hc_plan_materiel_other_config_exampleMapper;
import com.jy.adscm.hcPlan.service.HcPlanMakePartMaterielService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 零件制造标所需物料标准配置 服务实现类
 * </p>
 *
 * @author yyy
 * @since 2018-09-04
 */
@Service
public class HcPlanMakePartMaterielServiceImpl extends ServiceImpl<HcPlanMakePartMaterielMapper, HcPlanMakePartMateriel> implements HcPlanMakePartMaterielService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    Hc_plan_materiel_other_config_exampleMapper hc_plan_materiel_other_config_exampleMapper;
    @Autowired
    Hc_plan_materiel_other_configMapper hc_plan_materiel_other_configMapper;

    @Override
    public List<Hc_plan_materiel_other_config_example> getMaterielOtherConfig(String materiel_merge_id)throws AdscmSystemException {

        logger.info("获取单次并单的物料其他标准时间配置...");

        List<Hc_plan_materiel_other_config_example> list = null;

        try {
            if (AdscmBaseUtils.getInstance().isNotEmpty(materiel_merge_id,null)){

                Wrapper<Hc_plan_materiel_other_config_example> wrapper = new EntityWrapper<>();
                wrapper.eq("merge_puchase_id",materiel_merge_id);
                list = hc_plan_materiel_other_config_exampleMapper.selectList(wrapper);
            }

            if (!(list != null && list.size()>0)){
                //获取标准的
                List<Hc_plan_materiel_other_config> configs = hc_plan_materiel_other_configMapper.selectList(null);
                list = new ArrayList<>();
                if (configs != null && configs.size()>0){

                    for (Hc_plan_materiel_other_config config :
                            configs) {
                        Hc_plan_materiel_other_config_example cof = new Hc_plan_materiel_other_config_example();
                        cof.setCycle_day(config.getCycle_day());
                        cof.setConfig_name(config.getConfig_name());
                        list.add(cof);
                    }
                }

            }

        }catch (Exception e){
            logger.error("获取单次并单的物料其他标准时间配置异常",e);
            throw new AdscmSystemException(e.getMessage());
        }

        return list;
    }
}
